package com.quickask.action;

import com.opensymphony.xwork2.ActionSupport;
import com.quickask.entity.*;
import com.quickask.operate.data.Constants;
import com.quickask.operate.data.ResultCode;
import com.quickask.service.QuestionService;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/30.
 */
public class QuestionAction extends ActionSupport {
    private QuestionService questionService;
    private Map<String, Object> responseResult = new LinkedHashMap<String, Object>();
    private Integer pageNo;
    private Integer pageSize;
    private Integer orderType;
    private Integer questionId;
    private Integer userId;
    private String questionJson;
    private String token;
    private Double longitude;
    private Double latitude;
    private String blockCode;
    private Integer answerId;

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }


    //http://localhost:8080/QuickAsk/question/addQuestion.do
    //questionJson参数 {"userId": 3,"questionPoint": 5,"questionContent": "有人知道科学中心怎么去么？","questionDescription": "急！！！","altitude":0.56,"longitude":108.375445,"latitude":22.716363,"provinceCode":"44","cityCode":"4401","blockCode":"440113","locationDetail": "大学城南五路","questionTag":"问路"}
    public String addQuestion() throws JSONException {
        if (questionJson == null) {
            responseResult.put("data", null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.PARAMS_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.PARAMS_ERROR));
            return SUCCESS;
        }
        AsknowQuestionSet asknowQuestionSet = questionService.addQuestion(questionJson, token);

        if (null != asknowQuestionSet) {
            responseResult.put("data", asknowQuestionSet);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else {
            responseResult.put("data", null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/addQuestion.do?questionId=?&answerId=?
    public String addAdoptAnswer() {
        int resultCode = questionService.addAdoptAnswer(questionId,answerId);
        if(resultCode==ResultCode.SUCCESS) {
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.QUESTION_ADOPT_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_ADOPT_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/refreshList.do?pageNo=?&pageSize=?&questionId=?
    public String refreshList() {
        List<AsknowQuestionSet> questionList = null;//questionService.getQuestionListByAsktime(pageNo, pageSize, questionId == null ? 0 : questionId, null);
        if (!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("data", null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/requestList.do?pageNo=?&pageSize=?&latitude=?&longitude=?&blockCode=?&...
    public String requestList() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("latitude",latitude);
        properties.put("longitude",longitude);
        properties.put("blockCode",blockCode);

        List<AsknowQuestionSet> questionList = questionService.getQuestionListByGeohash(pageNo, pageSize, properties);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/requestQuestionList.do?pageNo=?&pageSize=?&...
    public String requestListByAsktime() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("latitude",latitude);
        properties.put("longitude",longitude);
        properties.put("blockCode",blockCode);

        List<AsknowQuestionSet> questionList = questionService.getQuestionListByAsktime(pageNo, pageSize, properties);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/requestListByPoint.do?pageNo=?&pageSize=?&latitude=?&longitude=?&blockCode=?&...
    public String requestListByPoint() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("latitude",latitude);
        properties.put("longitude",longitude);
        properties.put("blockCode",blockCode);

        List<AsknowQuestionSet> questionList = questionService.getQuestionListByQuestionPoint(pageNo, pageSize, properties);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/requestListByAsknum.do?pageNo=?&pageSize=?&latitude=?&longitude=?&blockCode=?&...
    public String requestListByAsknum() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("latitude",latitude);
        properties.put("longitude",longitude);
        properties.put("blockCode",blockCode);

        List<AsknowQuestionSet> questionList = questionService.getQuestionListByAsknum(pageNo, pageSize, properties);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    public String requestListByAnswernum() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("latitude",latitude);
        properties.put("longitude",longitude);
        properties.put("blockCode",blockCode);

        List<AsknowQuestionSet> questionList = questionService.getQuestionListByAnswernum(pageNo, pageSize, properties);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //http://localhost:8080/QuickAsk/question/requestQuestionListByUserId.do?pageNo=?&pageSize=?&userId=?&...
    public String requestListByUserId() {
        List<AsknowQuestionSet> questionList = questionService.getQuestionListByUserId(pageNo,pageSize,userId,null);
        if (questionList!=null&&!questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if (questionList!=null&&questionList.isEmpty()) {
            responseResult.put("data", questionList);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.QUESTION_LIST_EMPTY);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QUESTION_LIST_EMPTY));
        } else {
            responseResult.put("data",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    public Map<String, Object> getResponseResult() {
        return responseResult;
    }

    public void setResponseResult(Map<String, Object> responseResult) {
        this.responseResult = responseResult;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public Integer getQuestionId() {
        return questionId;
    }

    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getQuestionJson() {
        return questionJson;
    }

    public void setQuestionJson(String questionJson) {
        this.questionJson = questionJson;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public String getBlockCode() {
        return blockCode;
    }

    public void setBlockCode(String blockCode) {
        this.blockCode = blockCode;
    }

    public Integer getAnswerId() {
        return answerId;
    }

    public void setAnswerId(Integer answerId) {
        this.answerId = answerId;
    }
}
